package com.singer.piping.method;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.singer.domain.SingerEntity;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;

public class Result  {
    public    void pro() throws Exception {
        Properties prop = new Properties();

        prop.load(new FileInputStream("src/druid.properties"));

        DataSource data = DruidDataSourceFactory.createDataSource(prop);

        Connection connection = data.getConnection();
        String sql = "select*from t_singer";

        PreparedStatement sr = connection.prepareStatement(sql);
        ResultSet rs = sr.executeQuery();
        SingerEntity singer = null;
        List<SingerEntity> ser = new ArrayList<>();
        while (rs.next()) {
            int id = rs.getInt("id");
            String names = rs.getString("name");
            int age = rs.getInt("age");
            String sex = rs.getString("sex");
            String song = rs.getString("song");
            double score = rs.getDouble("score");
            singer = new SingerEntity();
            singer.setId(id);
            singer.setName(names);
            singer.setAge(age);
            singer.setSex(sex);
            singer.setSong(song);
            singer.setScore(score);
            ser.add(singer);
        }
        rs.close();
        sr.close();
        connection.close();
      ser.sort((o1, o2) ->  Double.compare(o2.getScore(),o1.getScore()));

        for (int i = 0; i < ser.size(); i++) {
            System.out.println("第"+(i+1)+"名:"+ser.get(i).getName()+":");
            System.out.println("演唱歌曲:"+"<"+ser.get(i).getSong()+">"+"得分为:"+ser.get(i).getScore());
        }

    }
}
